package com.jo.util;

import com.jo.anotation.MyComponent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.lang.annotation.Annotation;

/**
 * ioc容器中的一些工具类
 * @author Jo
 * @date 2018/1/25
 */
public class IocUtil {
    public static String getStandardName(Class clazz) {
        String name = clazz.getSimpleName();
        Assert.hasText(name, "name 不能为空");
        return name.substring(0, 1).toLowerCase() + name.substring(1);
    }

    public static String getMyComponentName(Class clazz) {
        String name = null;

        Annotation annotation = clazz.getAnnotation(MyComponent.class);
        if (annotation!= null && annotation instanceof MyComponent) {
            MyComponent myComponent = (MyComponent) annotation;
            name = myComponent.value();
        }
        if (StringUtils.isBlank(name)){
            name = IocUtil.getStandardName(clazz);
        }
        return name;
    }
}
